#pragma once

#include <iostream>
#include <fstream>
#include <string>

// 工具类

class Util
{
public:
    static bool ReadOneLine(std::string &in, std::string* out, const std::string &sep)
    {
        auto pos = in.find(sep);
        if (pos == std::string::npos)
            return false;
        
        *out = in.substr(0, pos);
        in.erase(0, pos + sep.size());
        return true;
    }

    static bool ReadFileContent(const std::string &filename, std::string *out)
    {
        // version2: 以二进制方式读取
        int filesize = FileSize(filename);
        if (filesize > 0)
        {
            std::ifstream in(filename);
            if (!in.is_open())
                return false;
            
            out->resize(filesize);
            in.read((char*)out->c_str(), filesize);
            in.close();
        }
        else
        {
            return false;
        }

        return true;
    }

    static int FileSize(const std::string &filename)
    {
        std::ifstream in(filename, std::ios::binary);
        if (!in.is_open())
            return -1;
        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);
        in.close();
        return filesize;
    }
};